﻿using System;
using System.Data;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;

namespace GUI.Controls.Sport
{
    public partial class ListNewsAsTimeline : System.Web.UI.UserControl
    {

        private const int ImageWidth = 120;
        private const int ImageHeight = 90;
        private const int ImageWidth2 = 200;
        private const int ImageHeight2 = 120;
        private const string Format = "<li>{3}<h2>{4}<label>{5}</label></h2><a href=\"{0}\" class=\"itemtitle\" title=\"{2}\">{1}{7}</a><p>{6}</p></li>";
        private const string GroupDateFormat = "<li><p class=\"group-date\">{0}</p></li>";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public bool DataBind(DataTable dt)
        {

            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new StringBuilder();
                var groupDate = default(DateTime);
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    var avatar = i % 4 == 0
                     ? PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth2, ImageHeight2, lazyLoad: true)
                     : PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth, ImageHeight, lazyLoad: true);

                    int subCatId;
                    try
                    {
                        subCatId = PresentationUtils.Object2Integer(r["SubZoneId"].ToString());
                    }
                    catch
                    {
                        subCatId = 0;
                    }

                    var publishDate = PresentationUtils.Object2DateTime(r[SqlConstants.NewsPublish.DistributionDate].ToString());
                    if (groupDate == default(DateTime) || !groupDate.ToShortDateString().Equals(publishDate.ToShortDateString()))
                    {
                        groupDate = publishDate;
                        sb.Append(string.Format(GroupDateFormat, publishDate.ToString("dd/MM/yyyy")));
                    }


                    sb.Append(string.Format(
                            Format,
                            r[SqlConstants.NewsPublish.Url],
                            r[SqlConstants.NewsPublish.Title],
                            PresentationUtils.EnCode(r[SqlConstants.NewsPublish.Title].ToString()),
                            avatar,
                            GetOwnCategory(subCatId),
                            publishDate.ToString("HH:mm"),
                            r[SqlConstants.NewsPublish.InitSapo],
                            r[SqlConstants.NewsPublish.NewsTypeIcon]
                        ));
                }

                ltrData.Text = sb.ToString();
                return true;
            }
            return false;
        }

        private string GetOwnCategory(int catId)
        {
            if (catId > 0)
            {
                var objCat = NewsBO.GetInstance().GetCategoryDetail(catId);
                return objCat == null ? string.Empty :
                    string.Format("<a href=\"{0}\" title=\"{1}\">{1}</a> - ", objCat.Url, objCat.Name);
            }
            return string.Empty;
        }
    }
}